package com.echo.week;

import java.util.concurrent.*;

/**
 * @Author echo
 * @Description
 * @Date 2023/10/22.
 * @Version 1.0
 */
public class DemoFutureTask {

    public static void main(String[] args) {

        Task task = new Task();
        FutureTask futureTask = new FutureTask(task);
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.submit(futureTask);
        try {
            System.out.println("task计算结果:"+futureTask.get());
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
        executorService.shutdown();
    }

    static  class Task implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            System.out.println("子线程正在计算.....");
            Thread.sleep(3000);
            int result =  0;
            for (int i = 1; i <=100 ; i++) {
                result +=i;
            }
            return result;
        }
    }
}
